package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {
    @DubboReference
    private AdminService adminService;
    /*渲染首页的控制器*/
    @GetMapping("/")
    public String index(ModelMap modelMap){
        //暂时写死成一个id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Admin admin = adminService.getByUsername(user.getUsername());
        List<Permission> permissionList=adminService.findMenuPermissionByAdminId(admin.getId());
        //当前登录的用户
        //查询当前用户的所有的权限  放入请求域
        modelMap.addAttribute("admin",admin);
        modelMap.addAttribute("permissionList",permissionList);

        return "frame/index";
    }


    /*渲染main页面的控制器*/
    @RequestMapping("/main")
    public String main(){
        return "frame/main";
    }

    @RequestMapping("/login")
    public String login(){
        return "frame/login";
    }


}
